package com.hjm.mall.service.impl;

import com.hjm.mall.api.param.OrderInsertParam;
import com.hjm.mall.api.param.OrderUpdateParam;
import com.hjm.mall.api.vo.OrderCreateVO;
import com.hjm.mall.api.vo.ShopCartVO;
import com.hjm.mall.dao.GoodsMapper;
import com.hjm.mall.dao.OrderMapper;
import com.hjm.mall.dao.StoreMapper;
import com.hjm.mall.entity.Goods;
import com.hjm.mall.entity.Order;
import com.hjm.mall.entity.Store;
import com.hjm.mall.service.OrderService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    StoreMapper storeMapper;

    @Override
    public int insert(Order order) {
        int id = orderMapper.insert(order);
        return id;
    }

    @Override
    public Boolean delete(Integer id) {
        int res = orderMapper.delete(id);
        return res > 0;
    }

    @Override
    public Boolean update(Order order) {
        int res = orderMapper.update(order);
        return res > 0;
    }

    @Override
    public Boolean updateState(Integer id, Integer state) {
        Order order = new Order();
        order.setId(id);
        order.setState(state);
        return orderMapper.update(order)>0;
    }

    @Override
    public Order getOrderById(Integer id) {
        return orderMapper.getOrderById(id);
    }

    @Override
    public List<Order> getOrderListByUserId(Long userId) {
        return orderMapper.getOrderListByUserId(userId);
    }

    @Override
    public List<ShopCartVO> getShopCartVOList(Long userId) {
        // 1 表示在购物车
        List<Order> orderList = orderMapper.orderListQuery(new Order(1, userId));
        List<ShopCartVO> shopCartVOList = new ArrayList<>();
        for (Order order : orderList) {
            ShopCartVO shopCartVO = new ShopCartVO();
            BeanUtils.copyProperties(order,shopCartVO);
            shopCartVOList.add(shopCartVO);
        }
        return shopCartVOList;
    }

    @Override
    public List<OrderCreateVO> getOrderCreateVOList(Long userId, int[] orderIds) {
        List<Integer> orderIdList = Arrays.stream(orderIds) // 将int数组转换为IntStream
                .boxed() // 将IntStream转换为Stream<Integer>
                .collect(Collectors.toList()); // 将Stream<Integer>转换为List<Integer>
        List<Order> orderList = orderMapper.getOrderListByUserIdAndOrderIds(userId, orderIdList);
        List<OrderCreateVO> orderCreateVOList = new ArrayList<>();
        OrderCreateVO orderCreateVO;
        for (Order order:orderList){
            orderCreateVO = new OrderCreateVO();
            BeanUtils.copyProperties(order,orderCreateVO);
            // 状态改成 2 表示未付款
            if (updateState(orderCreateVO.getOrderId(),2))
                orderCreateVOList.add(orderCreateVO);
        }
        return orderCreateVOList;
    }

    public Boolean update(OrderUpdateParam orderUpdateParam) {
        Order order = getOrderById(orderUpdateParam.getId());
        if (order == null)
            return false;
        BeanUtils.copyProperties(orderUpdateParam, order);
        // 设置类型
        if (orderUpdateParam.getGoodsType() != null) {
            Goods goods = goodsMapper.getGoodsById(order.getGoodsId());
            String[] typeArr = goods.getType();
            if (typeArr.length > orderUpdateParam.getGoodsType())
                order.setGoodsType(typeArr[orderUpdateParam.getGoodsType()]);
        }
        System.out.println(order);
        int res = orderMapper.update(order);
        return res > 0;
    }

    public Integer insert(Long userId, OrderInsertParam orderInsertParam) {
        Order order = new Order();
        BeanUtils.copyProperties(orderInsertParam, order);

        // 设置类型
        if (orderInsertParam.getGoodsType() != null) {
            Goods goods = goodsMapper.getGoodsById(order.getGoodsId());
            String[] typeArr = goods.getType();
            if (typeArr.length > orderInsertParam.getGoodsType())
                order.setGoodsType(typeArr[orderInsertParam.getGoodsType()]);
        }
        // 设置发货信息
        order.setTxAddr(2);

        // 判断是否存在订单，两个内容是否相同（状态、类型、收货地址），id可以不同
        Order orderByGoodsId = new Order();
        orderByGoodsId.setUserId(userId);
        orderByGoodsId.setGoodsId(order.getGoodsId());
        List<Order> orderList = orderMapper.getOrderByGoodsId(orderByGoodsId);
        if (orderList.size()!=0){
            for (Order tempOrder:orderList){
                if(tempOrder.getState() == order.getState()
                        && tempOrder.getGoodsType().equals(order.getGoodsType())
                        && tempOrder.getRxAddr()==order.getRxAddr()) {
                    System.out.println("新增订单：与已存在订单相同，数量+1");
                    tempOrder.setGoodsNum(tempOrder.getGoodsNum()+1);
                    orderMapper.update(tempOrder);
                    return tempOrder.getId();
                }
            }
        }

        System.out.println("新增订单：与已存在订单不相同");
        order.setUserId(userId);

        int res = orderMapper.insert(order);
        Integer id = order.getId();
        if (res>0&&id!=null){
            return id;
        }
        return null;
    }
}
